package edu.bbu.license.client.android.utility;

public class DelayedExecutor {

	public static final long DEFAULT_DELAY = 250;
	
	private long delayTime;
	private Runnable delayedTask;	
	private boolean dismissed = false;
	
	public DelayedExecutor(Runnable delayedTask) {
		this(DEFAULT_DELAY, delayedTask);
	}
	
	public DelayedExecutor(long delayTime, Runnable delayedTask) {
		this.delayTime = delayTime;
		this.delayedTask = delayedTask;
	}
	
	public synchronized void start() {
		dismissed = false;
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(delayTime);
				} catch (Exception e) { }
				
				synchronized (DelayedExecutor.this) {
					if(!dismissed) {
						delayedTask.run();
					}
				}
			}
		}).start();
	}
	
	public synchronized void dismiss() {
		dismissed = true;
	}
}
